package com.company.netty.demo;

import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class TestMyBuffer1 {

    public static void main(String[] args) {

        try {
            FileInputStream fileInputStream = new FileInputStream("data.txt");
            FileChannel channel = fileInputStream.getChannel();

            //设置缓冲区大小
            ByteBuffer buffer = ByteBuffer.allocate(10);

            while (true) {
                //从channel读取数据，向buffer中写入
                int read = channel.read(buffer);
                if(read==-1){
                    break;
                }

                //切换只读模式
                buffer.flip();

                //无法读取文件所有内容
                while (buffer.hasRemaining()) {
                    System.out.print((char) buffer.get());
                }

                //清空缓冲区
                buffer.clear();
            }
            fileInputStream.close();


        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }
}
